local skel = fk.CreateSkill {
  name = "rmt__jiezhu",
}

Fk:loadTranslationTable{
  ["rmt__jiezhu"] = "皆逐",
  [":rmt__jiezhu"] = "结束阶段，你可以选择一项：1.弃置一张进攻坐骑牌，获得一个额外回合；2.弃置一张武器牌，令一名其他角色翻面。",

  ["#rmt__jiezhu-choice"] = "皆逐：你可以弃置一张牌，执行对应效果",
  ["rmt__jiezhu_active"] = "皆逐",
  ["rmt__jiezhu1"] = "弃置进攻坐骑，获得额外回合",
  ["rmt__jiezhu2"] = "弃置武器牌，令一名角色翻面",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local success, dat = player.room:askToUseActiveSkill(player, {
      skill_name = "rmt__jiezhu_active",
      prompt =  "#rmt__jiezhu-choice",
    })
    if success and dat then
      event:setCostData(self, {cards = dat.cards, tos = dat.targets, choice = dat.interaction})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    room:throwCard(cost_data.cards, skel.name, player, player)
    if cost_data.choice == "rmt__jiezhu1" then
      if player.dead then return end
      player:gainAnExtraTurn(true, skel.name)
    else
      local to = cost_data.tos[1]
      if to and not to.dead then
        to:turnOver()
      end
    end
  end
})

local skel2 = fk.CreateSkill {
  name = "rmt__jiezhu_active",
}

skel2:addEffect("active", {
  card_num = 1,
  min_target_num = 0,
  max_target_num = 1,
  interaction = function()
    return UI.ComboBox {choices = {"rmt__jiezhu1", "rmt__jiezhu2"}}
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 and not player:prohibitDiscard(to_select) then
      if self.interaction.data == "rmt__jiezhu1" then
        return Fk:getCardById(to_select).sub_type == Card.SubtypeOffensiveRide
      else
        return Fk:getCardById(to_select).sub_type == Card.SubtypeWeapon
      end
    end
  end,
  target_filter = function (self, player, to_select, selected)
    if self.interaction.data == "rmt__jiezhu1" then
      return false
    else
      return #selected == 0 and to_select ~= player
    end
  end,
  feasible = function (self, player, selected, selected_cards)
    if #selected_cards == 1 then
      if self.interaction.data == "rmt__jiezhu1" then
        return #selected == 0
      else
        return #selected == 1
      end
    end
  end,
})

return {skel, skel2}
